import pygame
from pytmx import load_pygame

from actor import xiao_tie
from actor.bullet import Bullet
from actor.japanese_soldier import JapaneseWalkSoldier
from actor.prizes import PrizesSprite
from actor.red_army import RedArmyShootSoldier
from actor.tent import TentSprite
from actor.xiao_tie import XiaoTie
from scene import BaseScene, ScenePassStatus
from scene.task import TaskStatus
from scene.task.task1_take_prizes import TakePrizesTask
from scene.task.task2_kill_japanese import KillJapaneseTask
from scene.task.task3_boom_japanese_tent import BoomJapaneseTentTask
from scene.task.task4_transmit_message import TransmitMessageTask


class TunnelWarScene(BaseScene):
    def __init__(self, xiao_tie):
        pygame.mixer.music.load("resources/sound/十送红军.mp3")
        pygame.mixer.music.play()  # -1参数控制无限循环一首背景音乐
        super(TunnelWarScene, self).__init__(xiao_tie, "resources/tmx/Tunnel_War.png")
        self.tiled_map_data = load_pygame("resources/tmx/Tunnel_War_New.tmx")
        self.obstacle_group = pygame.sprite.Group()
        self.japanese_group = pygame.sprite.Group()
        self.japanese_tent_group = pygame.sprite.Group()
        self.prizes_group = pygame.sprite.Group()
        self.bullet_group = pygame.sprite.Group()
        self.init_actor()
        self.init_task()

    def init_actor(self):
        self.wang_gang = None
        self.li_yu = None

        for group in self.tiled_map_data.objectgroups:
            if group.name == "日军":
                for obj in group:
                    japanese = JapaneseWalkSoldier(obj.x, obj.y)
                    self.japanese_group.add(japanese)
            elif group.name == "道具":
                for obj in group:
                    prizes = PrizesSprite(obj.x, obj.y)
                    prizes.rect = pygame.Rect(obj.x, obj.y, obj.width, obj.height)
                    self.prizes_group.add(prizes)
            elif group.name == "空气墙":
                for obj in group:
                    obstacle = pygame.sprite.Sprite()
                    obstacle.rect = pygame.Rect(obj.x, obj.y, obj.width, obj.height)
                    self.obstacle_group.add(obstacle)
            elif group.name == "红军":
                for obj in group:
                    if obj.name == "王刚":
                        self.wang_gang = RedArmyShootSoldier(obj.x, obj.y)
                    if obj.name == "李玉":
                        self.li_yu = RedArmyShootSoldier(obj.x, obj.y)
            elif group.name == "小铁":
                for obj in group:
                    if obj.name == "小铁":
                        self.xiao_tie.set_pos(obj.x, obj.y)
            elif group.name == "日军帐篷":
                for obj in group:
                    tent = TentSprite(obj.x, obj.y)
                    tent.rect = pygame.Rect(obj.x, obj.y, obj.width, obj.height)
                    self.japanese_tent_group.add(tent)

    def init_task(self):
        self.font = pygame.font.Font("resources/font/FZSTK.TTF", 20)
        self.tasks = []
        self.tasks.append(TakePrizesTask(self.xiao_tie, self.prizes_group, self.font))
        self.tasks.append(KillJapaneseTask(self.xiao_tie, self.japanese_group, self.font))
        self.tasks.append(BoomJapaneseTentTask(self.xiao_tie, self.japanese_tent_group, self.font))
        self.tasks.append(TransmitMessageTask(self.xiao_tie, self.wang_gang, self.li_yu, self.font))
        self.task_id = 0

    def draw_actor(self):
        self.xiao_tie.draw(self.current_surface, self.view_x, self.view_y)
        for japanese in self.japanese_group:
            japanese.draw(self.current_surface, self.view_x, self.view_y)
        # self.wang_gang.draw(self.current_surface, self.view_x, self.view_y)
        # self.li_yu.draw(self.current_surface, self.view_x, self.view_y)
        for prizes in self.prizes_group:
            prizes.draw(self.current_surface, self.view_x, self.view_y)
        # for obj in self.obstacle_group:
        #     x, y, w, h = obj.rect
        #     pygame.draw.rect(self.current_surface, pygame.Color(255, 0, 0),
        #                      (x - self.view_x, y - self.view_y, w, h), 1)

        self.tasks[self.task_id].draw(self.current_surface, self.view_x, self.view_y)
        for bullets in self.bullet_group:
            bullets.draw_bullet(self.current_surface, self.view_x, self.view_y)

    def run(self, down_flag, key_list):
        self.xiao_tie.run(down_flag, key_list, self.obstacle_group, self.japanese_group)
        if down_flag and key_list[pygame.K_h]:
            Bullets = Bullet(self.xiao_tie)
            self.bullet_group.add(Bullets)
        for bullets in self.bullet_group:
            bullets.run()
        for japanese in self.japanese_group:
            japanese.run(down_flag, key_list, self.obstacle_group, self.bullet_group)

        self.tasks[self.task_id].do_task(down_flag, key_list)
        task_status = self.tasks[self.task_id].get_state()
        if task_status == TaskStatus.win_over:
            self.task_id += 1
            if self.task_id >= len(self.tasks):
                self.pass_status = ScenePassStatus.final_win
                pygame.mixer.music.stop()
        elif task_status == TaskStatus.fail_over:
            self.pass_status = ScenePassStatus.final_fail
            pygame.mixer.music.stop()


    def get_pass_status(self):
        if self.pass_status == ScenePassStatus.final_win:
            return self.pass_status, "WinScene"
        elif self.pass_status == ScenePassStatus.final_fail:
            return self.pass_status, "FailScene"
        else:
            return self.pass_status, None